<template>
  <div>
    <div class="base_header" style="text-align: left;margin-bottom: 20px">
      <span style="font-size: 30px;font-weight: normal;">
        分类管理
      </span>
      <span style="font-size: 10px">课程分类管理</span>
      <el-button type="success"
                 style="float: right"
                 @click="dialogVisible = true"
      >新增</el-button>
    </div>

    <el-dialog
      title="新增分类"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form label-position="left" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="newSort.name" placeholder="请输入方向名称">
          </el-input>
        </el-form-item>
        <el-form-item label="课程方向">
          <el-select v-model="newSort.directionId"
                     filterable
                     style="width: 100%"
                     placeholder="请选择">
            <el-option
              v-for="item in directions"
              :key="item.directionId"
              :label="item.name"
              :value="item.directionId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button @click="addCancle">取 消</el-button>
        <el-button type="primary" @click="addSave">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      :data="sortList"
      style="width: 100%">
      <el-table-column
        label="序号"
        width="80">
        <template slot-scope="scope">
          <span>
            {{scope.$index + 1 +  (page - 1) * pageSize}}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="名称">
        <template slot-scope="scope">
          <el-input v-model="scope.row.sortName" v-if="showEdit[scope.$index]"></el-input>
          <span v-else>
            {{scope.row.sortName}}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="方向">
        <template slot-scope="scope">
          <el-select v-model="scope.row.directionId"
                     filterable
                     v-if="showEdit[scope.$index]"
                     placeholder="请选择">
            <el-option
              v-for="item in directions"
              :key="item.directionId"
              :label="item.name"
              :value="item.directionId">
            </el-option>
          </el-select>
          <span v-else>
            {{scope.row.directionName}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div v-if="!showEdit[scope.$index]">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-popconfirm
              title="确定删除吗？"
              @onConfirm="handleDelete(scope.$index, scope.row)"
            >
              <el-button
                size="mini"
                slot="reference"
                type="danger"
              >删除</el-button>
            </el-popconfirm>
          </div>
          <div v-else>
            <el-button
              size="mini"
              type="danger"
              @click="handleCancle(scope.$index, scope.row)">取消</el-button>
            <el-button
              size="mini"
              type="success"
              @click="handleSave(scope.$index, scope.row)">保存</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination" style="text-align: center">
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        :hide-on-single-page="true"
        layout="prev, pager, next"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SortManage',
  data () {
    return {
      total: 0,
      page: 1,
      pageSize: 10,
      dialogVisible: false,
      directions: [],
      newSort: {
        directionId: '',
        name: ''
      },
      showEdit: [],
      linSort: {},
      sortList: []
    }
  },
  mounted () {
    this.getAllDirection()
    this.getSizeSort()
    this.getListSort(1, this.pageSize)
  },
  methods: {
    getListSort (page, pageSize) {
      var _this = this
      var commonQueryData = {
        page: page,
        pageSize: pageSize
      }
      this.$axios.post('/api/sort/getListSort',
        this.$qs.stringify({
          queryData: JSON.stringify(commonQueryData)
        }))
        .then(function (response) {
          var result = response.data
          if (result.isSuccess) {
            _this.sortList = result.result
            var len = _this.sortList.length
            _this.showEdit.splice(0)
            for (let i = 0; i < len; i++) {
              _this.showEdit.push(false)
            }
          }
        })
    },
    getSizeSort () {
      var _this = this
      this.$axios.post('/api/sort/getSizeSort')
        .then(function (response) {
          var result = response.data
          if (result.isSuccess) {
            _this.total = parseInt(result.result)
          }
        })
    },
    // 下方分页点击
    handleCurrentChange (page) {
      this.page = page
      this.getListSort(page, this.pageSize)
    },
    handleEdit (index, row) {
      this.$set(this.showEdit, index, true)
      this.linSort = JSON.parse(JSON.stringify(row))
    },
    handleCancle (index, row) {
      this.sortList.splice(index, 1, this.linSort)
      this.$set(this.showEdit, index, false)
    },
    handleSave (index, row) {
      var _this = this
      var sortData = {}
      sortData.sortId = row.sortId
      sortData.name = row.sortName
      sortData.directionId = row.directionId
      // eslint-disable-next-line eqeqeq
      if (row.sortName != this.linSort.sortName || row.directionId != this.linSort.directionId) {
        this.$set(this.showEdit, index, false)
        this.$axios.post('/api/sort/update',
          this.$qs.stringify({
            sortData: JSON.stringify(sortData)
          })
        )
          .then(function (response) {
            var data = response.data
            if (data.isSuccess) {
              _this.$message({
                type: 'success',
                message: '更新成功！',
                offset: 50
              })
              for (let i = 0; i < _this.directions.length; i++) {
                let direction = _this.directions[i]
                // eslint-disable-next-line eqeqeq
                if (direction.directionId == row.directionId) {
                  row.directionName = direction.name
                  break
                }
              }
              _this.$set(_this.showEdit, index, false)
            } else {
              _this.$message({
                type: 'error',
                message: '更新失败，请重试！',
                offset: 50
              })
            }
          })
      }
    },
    addCancle () {
      this.dialogVisible = false
    },
    addSave () {
      var _this = this
      this.$axios.post('/api/sort/add',
        this.$qs.stringify({
          sortData: JSON.stringify(this.newSort)
        })
      ).then(function (response) {
        var data = response.data
        if (data.isSuccess) {
          _this.$message({
            type: 'success',
            message: '添加成功',
            offset: 50
          })
          _this.dialogVisible = false
          _this.getSizeSort()
          _this.getListSort(_this.page, _this.pageSize)
        } else {
          _this.$message({
            type: 'error',
            message: '添加失败，请重试！',
            offset: 50
          })
        }
      }).catch(function (eachCallback) {
        _this.$message({
          type: 'error',
          message: '添加失败，请重试！',
          offset: 50
        })
      })
    },
    handleDelete (index, row) {
      var _this = this
      var sortData = {}
      sortData.sortId = row.sortId
      sortData.name = row.sortName
      sortData.directionId = row.directionId
      this.$axios.post('/api/sort/delete',
        this.$qs.stringify({
          sortData: JSON.stringify(sortData)
        })
      )
        .then(function (response) {
          var data = response.data
          if (data.isSuccess) {
            _this.$message({
              type: 'success',
              message: '删除成功',
              offset: 50
            })
            _this.getSizeSort()
            _this.getListSort(_this.page, _this.pageSize)
            // eslint-disable-next-line eqeqeq
          } else if (data.code == 502) {
            _this.$message({
              type: 'error',
              message: '删除失败，该分类下还有所属课程，请您检查',
              offset: 50
            })
          }
        })
    },
    getAllDirection () {
      var _this = this
      this.$axios.post('/api/direction/getAllDirection')
        .then(function (response) {
          var data = response.data
          if (data.isSuccess) {
            _this.directions = data.result
          }
        })
    }
  }
}
</script>

<style scoped>

</style>
